/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trunk.view.ColorChooser;



import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import trunk.model.Dessin;
import trunk.view.Paint;


/**
 * Class ColorChooserView
 * 
 * Partie graphique du composant colorChooser
 * 
 * @author LEFEVRE Michael
 * @author THOUE Jonathan
 */
public class ColorChooserView extends JFrame{
  
    protected Paint principal;
    
    private JSlider sliderRed ;
    private JSlider sliderGreen ;
    private JSlider sliderBlue ;
    
    private JLabel currentRed;
    private JLabel currentGreen;
    private JLabel currentBlue;
    
    private JPanel newColor;
    
    JButton addButton;
    JButton cancelButton;

    /**
     * Constructeur de la classe
     */
    public ColorChooserView() {
     super("Gestionnaire des Couleurs");
     setVisible(true);     
     setPreferredSize(new Dimension(280, 180));
     setResizable(false);
     pack();
     JPanel mainPanel =new JPanel();
     JPanel panelRed =new JPanel();
     JPanel panelGreen =new JPanel();
     JPanel panelBlue =new JPanel();
     newColor = new JPanel();
     newColor.setBackground(Color.white);
     newColor.setPreferredSize(new Dimension(30, 20));
     
     
     sliderRed = new JSlider(JSlider.HORIZONTAL, 0, 255, 255);
     currentRed = new JLabel("0");
     panelRed.add(new JLabel("Rouge"));
     panelRed.add(sliderRed);
     panelRed.add(currentRed);
     
     sliderGreen = new JSlider(JSlider.HORIZONTAL, 0, 255, 255);
     currentGreen = new JLabel("0");
     panelGreen.add(new JLabel("Vert"));
     panelGreen.add(sliderGreen);
     panelGreen.add(currentGreen);
     
     sliderBlue = new JSlider(JSlider.HORIZONTAL, 0, 255, 255);
     currentBlue = new JLabel("0");
     panelBlue.add(new JLabel("Bleu"));
     panelBlue.add(sliderBlue);
     panelBlue.add(currentBlue);
     
     addButton = new JButton("Ajouter");
     cancelButton = new JButton("Annuler");
     
     mainPanel.add(panelRed);
     mainPanel.add(panelGreen);
     mainPanel.add(panelBlue);
     mainPanel.add(newColor);     
     mainPanel.add(addButton);
     mainPanel.add(cancelButton);
     add(mainPanel);
     validate();
    }
    /**
     * Get newColor
     * @return le panel de la couleur composée
     */
    public JPanel getNewColor() {
        return newColor;
    }
    /**
     *Methode pour abonner les slider a leur listenner
     * @param ctrl - listenner du composant
     */
    public void setColorChooserCtrl(ColorChooserCtrl ctrl){
        sliderRed.addChangeListener(ctrl);
        sliderGreen.addChangeListener(ctrl);
        sliderBlue.addChangeListener(ctrl);
      
    }
    /**
     *Methode pour abonner les bouton a leur listenner
     * @param ctrl - listenner du composant
     */
    public void setColorChooserAddCtrl(final ColorChooserAddCtrl ctrl){
        addButton.addActionListener(ctrl);
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                ctrl.getView().setVisible(false);
            }
        });
    }
    /**
     * Get Principal
     * @return le lien vers la vue principal
     */
    public Paint getPrincipal() {
        return principal;
    }
    
    /**
     * Méthode de mise à jour de la vue
     * @param dessin - model general 
     */
    public void update(Dessin dessin) {
        
    }
    
    /**
     * Controlleur pour les sliders
     */
    public class ColorChooserCtrl implements ChangeListener{

    private ColorChooser    view;
    /**
     * Constructeur de la classe
     * @param view - lien vers la vue du composant
     */
    public ColorChooserCtrl(ColorChooser view) {
        this.view = view;
    }
    /**
     * Get view
     * @return vue du composant
     */
    public ColorChooser getView() {  return view;}

    /**
     * Evenement de changement d'etat
     * @param arg0 - evenement de changement
     */
    @Override
    public void stateChanged(ChangeEvent arg0) {

        if(arg0.getSource() == sliderRed)
            currentRed.setText(String.valueOf(((JSlider)arg0.getSource()).getValue()));        
        else if(arg0.getSource() == sliderGreen)
            currentGreen.setText(String.valueOf(((JSlider)arg0.getSource()).getValue()));        
        else if(arg0.getSource() == sliderBlue)
           currentBlue.setText(String.valueOf(((JSlider)arg0.getSource()).getValue())); 
        Color couleurPerso =new Color(Integer.parseInt(currentRed.getText()), Integer.parseInt(currentGreen.getText()), Integer.parseInt(currentBlue.getText()));
        newColor.setBackground(couleurPerso);        
    }
    
}
}
